package com.bj58.ecdata.dashboard.controllers;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

import com.bj58.ecdata.dashboard.databll.CustomerBLL;
import com.bj58.ecdata.dashboard.utils.MyServletRequestUtils;
import com.bj58.wf.mvc.ActionResult;
import com.bj58.wf.mvc.MvcController;
import com.bj58.wf.mvc.annotation.POST;
import com.bj58.wf.mvc.annotation.Path;

@Path("/income/ajax")
public class CustomerAjaxController extends MvcController {

	/**
	 * 获取余额客户开通数据
	 * @return
	 */
	@Path("/customerNumForType")
	@POST
	public ActionResult getCustomerNumData() {
		try {
			HttpServletRequest request = beat.getRequest();
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "stat_date");
			String stat_type = MyServletRequestUtils.getStringParameter(request, "statType", "day");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","");
			String customerType = MyServletRequestUtils.getStringParameter(request, "customerType", "user_num");
			String dateVal = MyServletRequestUtils.getStringParameter(request, "dateVal", "2014-01-01");
			
			String dataType = MyServletRequestUtils.getStringParameter(request, "dataType", "chart");
			
			String data="";
			if(StringUtils.equalsIgnoreCase(dataType, "chart")){
				data = CustomerBLL.getCustomerNumJson(dateGroup, stat_type, businessName, customerType);
			}else if(StringUtils.equalsIgnoreCase(dataType, "table")){
				data = CustomerBLL.getCustomerProTableJson(dateGroup, stat_type, businessName, dateVal);
			}
//			log.info(businessName+"--"+cityName+"\n"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}
	/**
	 * 获取余额客户开通数据
	 * @return
	 */
	@Path("/customerNumForIndex")
	@POST
	public ActionResult getCustomerNumDataForIndex() {
		try {
			HttpServletRequest request = beat.getRequest();
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "stat_date");
			String stat_type = MyServletRequestUtils.getStringParameter(request, "statType", "day");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","");
			String customerIndex = MyServletRequestUtils.getStringParameter(request, "customerindex", "balance_0");
			String dateVal = MyServletRequestUtils.getStringParameter(request, "dateVal", "2014-01-01");
			
			String dataType = MyServletRequestUtils.getStringParameter(request, "dataType", "chart");
			
			String data="";
			if(StringUtils.equalsIgnoreCase(dataType, "chart")){
				data = CustomerBLL.getCustomerNumJsonForIndex(dateGroup, stat_type, businessName, customerIndex);
			}else if(StringUtils.equalsIgnoreCase(dataType, "table")){
				data = CustomerBLL.getCustomerProTableJson(dateGroup, stat_type, businessName, dateVal);
			}
//			log.info(businessName+"--"+cityName+"\n"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}
	/**
	 * 获取开通百分比
	 * @return
	 */
	@Path("/customerNumForPer")
	@POST
	public ActionResult getCustomerNumDataForPer() {
		try {
			HttpServletRequest request = beat.getRequest();
			
			String stat_type = MyServletRequestUtils.getStringParameter(request, "statType", "day");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","");
			String customerIndex = MyServletRequestUtils.getStringParameter(request, "customerType", "0");
			String dateVal = MyServletRequestUtils.getStringParameter(request, "dateVal", "2014-01-01");
			String data="";
			data = CustomerBLL.getCustomerNumJsonForPer(dateVal, stat_type, businessName, customerIndex);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}
	/**
	 * 获取余额客户漏斗数据
	 * @return
	 */
	@Path("/customerFunnelData")
	@POST
	public ActionResult getCustomerNumDataForFunnel() {
		try {
			HttpServletRequest request = beat.getRequest();
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "stat_date");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","");
			String dateVal = MyServletRequestUtils.getStringParameter(request, "selectDate", "2015-01-10");
			String data="";			
			data = CustomerBLL.getCustomerNumJsonForFunnel(dateGroup, dateVal, businessName);
			
//			log.info(businessName+"--"+cityName+"\n"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}
	/**
	 * 获取新增流失客户数据
	 * @return
	 */
	@Path("/customerNewData")
	@POST
	public ActionResult getCustomerNumDataForNew() {
		try {
			HttpServletRequest request = beat.getRequest();
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","");
			String customersource = MyServletRequestUtils.getStringParameter(request, "customerSource", "jingzhun");
			String daytype = MyServletRequestUtils.getStringParameter(request, "dayType","week");
			String dateVal = MyServletRequestUtils.getStringParameter(request, "dateVal", "2014-01-01");
            String chartType = MyServletRequestUtils.getStringParameter(request, "dataType", "chart");
            if("week_num".equals(daytype))
			{
				daytype="week";
			}else
			{
				daytype="month";
			}
			String data="";
			if(StringUtils.equalsIgnoreCase(chartType, "chart")){
				data = CustomerBLL.getCustomerNumJsonForNew(businessName,customersource, daytype);
			}else if(StringUtils.equalsIgnoreCase(chartType, "table")){
				data = CustomerBLL.getCustomerProTableJsonForNew(businessName,dateVal,daytype);
			}			
//			log.info(businessName+"--"+cityName+"\n"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}
	/**
	 * 获取现金余额与消耗周期散点图数据
	 * @return
	 */
	@Path("/customerCashcircleData")
	@POST
	public ActionResult getCustomerNumDataForCashcircle() {
		try {
			HttpServletRequest request = beat.getRequest();
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "week_num");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","");
			String daytype = MyServletRequestUtils.getStringParameter(request, "statType","week");
			String dateVal = MyServletRequestUtils.getStringParameter(request, "selectDate", "");
			String data="";			
			data = CustomerBLL.getCustomerNumJsonForCashcircle(dateGroup, dateVal, businessName,daytype);			
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}
	/**
	 * 获取消费余额散点图数据
	 * @return
	 */
	@Path("/customerCostbalanceData")
	@POST
	public ActionResult getCustomerNumDataForCostbalance() {
		try {
			HttpServletRequest request = beat.getRequest();
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "week_num");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","");
			String customersource = MyServletRequestUtils.getStringParameter(request, "customerType", "jingzhun");
			String daytype = MyServletRequestUtils.getStringParameter(request, "statType","week");
			String dateVal = MyServletRequestUtils.getStringParameter(request, "selectDate", "");
			String data="";			
			data = CustomerBLL.getCustomerNumJsonForCostbalance(dateGroup, dateVal, businessName,customersource,daytype);			
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}
	/**
	 * 获取置顶转换等散点图数据
	 * @return
	 */
	@Path("/customerCosttransferData")
	@POST
	public ActionResult getCustomerNumDataForCosttransfer() {
		try {
			HttpServletRequest request = beat.getRequest();
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "week_num");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","");
			String customersource = MyServletRequestUtils.getStringParameter(request, "customerType", "jingzhun");
			String daytype = MyServletRequestUtils.getStringParameter(request, "statType","week");
			String dateVal = MyServletRequestUtils.getStringParameter(request, "selectDate", "");
			String data="";			
			data = CustomerBLL.getCustomerNumJsonForCosttransfer(dateGroup, dateVal, businessName,customersource,daytype);			
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}
	/**
	 * 获取展现消费客户数据
	 * @return
	 */
	@Path("/customerNumForDispcost")
	@POST
	public ActionResult getCustomerNumDataForDispcost() {
		try {
			HttpServletRequest request = beat.getRequest();
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","");
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "stat_date");
			String customersource = MyServletRequestUtils.getStringParameter(request, "dispcostproduct", "jingzhun");
			String daytype = MyServletRequestUtils.getStringParameter(request, "statType","day");
			String dateVal = MyServletRequestUtils.getStringParameter(request, "dateVal", "2014-01-01");
            String chartType = MyServletRequestUtils.getStringParameter(request, "dataType", "chart");
			String data="";
			if("week_num".equals(dateGroup))
			{
				dateGroup="stat_date";
			}
			if(StringUtils.equalsIgnoreCase(chartType, "chart")){
				data = CustomerBLL.getCustomerNumJsonForDispcost(dateGroup, daytype, businessName, customersource);
			}else if(StringUtils.equalsIgnoreCase(chartType, "table")){
				data = CustomerBLL.getCustomerTableJsonForDispcost(dateGroup,daytype,businessName,dateVal);
			}			
//			log.info(businessName+"--"+cityName+"\n"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}
	/**
	 * 获取精准置顶转换客户数据
	 * @return
	 */
	@Path("/customerNumForTransfer")
	@POST
	public ActionResult getCustomerDataForTransfer() {
		try {
			HttpServletRequest request = beat.getRequest();
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","");
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "week_num");
			String customersource = MyServletRequestUtils.getStringParameter(request, "customerSource", "jingzhun");
			String daytype = MyServletRequestUtils.getStringParameter(request, "statType","week");
			String dateVal = MyServletRequestUtils.getStringParameter(request, "dateVal", "2014-01-01");
            String chartType = MyServletRequestUtils.getStringParameter(request, "dataType", "chart");
			String data="";
			if(StringUtils.equalsIgnoreCase(chartType, "chart")){
				data = CustomerBLL.getCustomerNumJsonForTransfer(dateGroup, daytype, businessName, customersource);
			}else if(StringUtils.equalsIgnoreCase(chartType, "table")){
				data = CustomerBLL.getCustomerTableJsonForTransfer(dateGroup,daytype,businessName,dateVal);
			}			
//			log.info(businessName+"--"+cityName+"\n"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}
}
